<!--数据库配置 add by gaojingyu-->
<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters" @submit.native.prevent>
                <el-form-item>
                    <el-input v-model="filters.name"   prefix-icon="el-icon-search" placeholder="数据表配置名称" @keyup.enter.native="getTableList"></el-input>
                </el-form-item>
                    <el-button type="primary" @click="getTableList">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>

            </el-form>
        </el-col>
        <!--列表-->
        <el-table :data="tableList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" min-width="5%">
            </el-table-column>
            <el-table-column type="index" align="center" :index='indexMethod' label="序号" />
            <el-table-column prop="id" label="id" min-width="5%" :show-overflow-tooltip="true">
            </el-table-column>


            <el-table-column prop="name" label="名称" min-width="15%" :show-overflow-tooltip="true">
            </el-table-column>

             <el-table-column prop="table_name" label=" 数据表名" min-width="15%" :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column prop="condition" label=" 条件" min-width="15%" :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column prop="conditionValue" label=" 条件值" min-width="15%" :show-overflow-tooltip="true">
            </el-table-column>
             <el-table-column prop="content" label=" 备注" min-width="15%" :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column label="操作" min-width="25%">
                <template slot-scope="scope">
                 <el-button  plain  type="success"  size="small" @click="handleInfo(scope.$index, scope.row)">详情</el-button>
                </template>
            </el-table-column>


        </el-table>
           <!--工具条-->
        <el-col :span="24" class="toolbar">
         <el-button  plain type="danger" size="small"  @click="back" >返回</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :page-count="total" style="float:right;">
            </el-pagination>
        </el-col>

         <!--新增界面-->



        <el-dialog title="新增数据表配置" :visible.sync="addFormVisible" :close-on-click-modal="false" customClass="customWidth" style="width: 80% left: 2%" >
            <el-form :model="addForm" label-width="120px" :rules="addFormRules" ref="addForm">
                <el-form-item label="名称:" prop="name">
                    <el-input v-model.trim="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="数据库:" prop="database">
                    <el-input v-model.trim="addForm.database" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="数据表:" prop="table_name">
                    <el-input v-model.trim="addForm.table_name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="条件:" prop="condition">
                    <el-input v-model.trim="addForm.condition"  placeholder="多个条件用英文逗号','分隔" auto-complete="off"></el-input>
                </el-form-item>


                 <el-row :gutter="18">
                    <el-col :span='18'>
                       <el-form-item label="删除方式:" label-width="83px" >
                           <template>
                               <el-radio-group v-model="radio3" @change="radioChange">
                                  <el-radio-button label="0" border>自动删除</el-radio-button>
                                  <el-radio-button label="1" border>手动配置</el-radio-button>
                               </el-radio-group>
                           </template>
                          <br/><a class="color-red">注:1 自动删除需配置条件值，多个条件及条件值用逗号分割<br/>      &nbsp &nbsp &nbsp &nbsp2手动配置无需设置条件值，执行删除操作时，手动输入条件值</a>


                        </el-form-item>
                    </el-col>
                </el-row>
                 <el-form-item label="条件值:" prop="conditionValue" :span='18'>
                    <el-input v-model.trim="addForm.conditionValue" :disabled="radio3==='1'"  placeholder="多个条件值用英文逗号','分隔  数量需与条件相同" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="备注:" prop="content" :span='18'>
                    <el-input v-model.trim="addForm.content"  ></el-input>
                </el-form-item>



            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    //import NProgress from 'nprogress'

   import { getTable,getApiList,editStatus_case,getApiListByProjectID,getProjectList,test
         ,addDatabase,addTable

       } from '../../../../api/api';

    // import ElRow from "element-ui/packages/row/src/row";
    export default {
        // components: {ElRow},
        data() {
            return {
            conditionValue:'',
              radio3:'0',

                filters: {
                    name: ''
                },
                inputText:'',
                detailFormVisible:false,
                limitNum:1,
                project: [],
                tableList:[],
                total: 0,
                totalCount:0,
                page: 1,
                listLoading: false,
                uploadurl:test + '/api/api/CaseExcleImport',
                sels: [],//列表选中列
                fileList: [],
                uploadform: false,
               // getAPIFirstID=[], //getProjectFirstID:'',
               // getAPIFirstID:'',//获取接口
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                form: {
                 getProjectFirstID:'',
                    getAPIFirstIDS:''},
                //options: [{ label: "Web", value: "Web"}, { label: "App", value: "App"}],


                //编辑界面数据
                editForm: {
                    name: '',
                    version: '',
                    type: '',
                    description: ''
                },
                templateDownload:false,
                addFormVisible: false,//新增界面是否显示
                addLoading: false,

                //新增界面数据
                addForm: {
                    name: '',
                    version: '',
                    type: '',
                    description: ''
                }

            }
        },
        methods: {
            // 获取项目列表 getLyzdEnvironments getLyzdEnvironments getLyzdCaseList  interface_id
           getTableList() {
                this.listLoading = true;
                let self = this;
                let params = {
                page: self.page,
               // interface_id: self.filters.interface_id,

                name:self.filters.name
                };


                let headers = {Authorization: 'Token '+JSON.parse(sessionStorage.getItem('token'))};
                getTable(headers, params).then((res) => {
                    self.listLoading = false;
                    let { msg, code, data } = res;
                    if (code === '999999') {
                        self.total = data.total;
                        self.tableList = data.data
                        this.totalCount=data.data.length
                    }
                    else {
                        self.$message.error({
                            message: msg,
                            center: true,
                        })
                    }
                })
            },
               indexMethod(index) {
            let curpage = this.page //单前页码，具体看组件取值

            let limitpage = 20//每页条数，具体是组件取值

            return (index + 1) + (curpage - 1) * limitpage

        },
             //跳转用
            handleResult: function (index, row) {

               let case_id=row.id
               this.$router.push({path:'/case_edit/cid='+case_id})
            },

            handleInfo: function (index, row) {
            this.detailFormVisible=true










            },
             back(){
                 this.$router.go(-1)

                },




            // 改变项目状态
            handleChangeStatus: function(index, row) {
                let self = this;
                this.listLoading = true;
                let params = { case_id: row.id};


                let headers = {Authorization: 'Token '+JSON.parse(sessionStorage.getItem('token'))};
                    editStatus_case(headers, params).then(_data => {
                        let { msg, code, data } = _data;
                        self.listLoading = false;
                        if (code === '999999') {
                            self.$message({
                                message: msg,
                                center: true,
                                type: 'success'
                            });

                            row.delete_flag =!row.delete_flag
                        }
                        else {
                            self.$message.error({
                                message: msg,
                                center: true,
                            })
                        }
                    });


            },

            handleCurrentChange(val) {
                this.page = val;
                this.getTableList()
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
            },


            selsChange: function (sels) {
                this.sels = sels;
            },
             radioChange(val){

            this.radio3=val
            if(val=='0'){

            }else{
            condition=''
            }

            },
              //新增数据表配置
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        let self = this;
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            self.addLoading = true;
                            //NProgress.start();
                            let params = JSON.stringify({
                                name: self.addForm.name,
                                table_name: self.addForm.table_name,
                                condition: self.addForm.condition,
                                database: self.addForm.database,
                                content: self.addForm.content,

                                conditionValue: self.addForm.conditionValue,

                            });



                            let header = {
                                "Content-Type": "application/json",
                                Authorization: 'Token '+JSON.parse(sessionStorage.getItem('token'))
                            };
                            addTable(header, params).then(_data => {
                                let {msg, code, data} = _data;
                                self.addLoading = false;
                                if (code === '999999') {
                                    self.$message({
                                        message: '添加成功',
                                        center: true,
                                        type: 'success'
                                    });
                                    self.$refs['addForm'].resetFields();
                                    self.addFormVisible = false;
                                    self.getTableList()
                                } else if (code === '999997') {
                                    self.$message.error({
                                        message: msg,
                                        center: true,
                                    })
                                } else {
                                    self.$message.error({
                                        message: msg,
                                        center: true,
                                    });
                                    self.$refs['addForm'].resetFields();
                                    self.addFormVisible = false;
                                    self.getTableList()
                                }
                            })
                        });
                    }
                });
            },



        },
        mounted() {

            this.getTableList();

        }
    }

</script>


<style>
/deep/.el-dialog__wrapper {
    //margin: 1px 16px 0;
    position: absolute;
    z-index: 1 !important;
  /deep/.el-dialog__header {
    padding: 10px;
    border-bottom: 1px solid #f7f4f4;
    /deep/.el-dialog__title {
      line-height: 10px;
      color: #303133;
      font-size: 15px;
      font-weight: 800;
    }
  }
  /deep/.el-dialog__body {
    padding: 10px;
    height: calc(100% - 37px);
  }
  /deep/.el-dialog__headerbtn {
    top: 10px;
  }
}

.customWidth{
        width:80%;
    }

.color-red{
   color: red;
   border: 1px solid #e8e8e8;
   padding: 0px 6px 0 9px;
   margin-right: 23px;
}
</style>

